package com.huawei.hianalytics.f.b;

import android.text.TextUtils;
import com.huawei.hianalytics.a.c;
import com.huawei.hianalytics.f.g.d;
import com.huawei.hianalytics.f.g.i;
import com.huawei.hianalytics.g.b;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class g {
    private String a;
    private h b;
    private i c;
    private b d;
    private a[] e;
    private String f;
    private String g;

    public g(String str, String str2, String str3) {
        this.a = str;
        this.f = str2;
        this.g = str3;
    }

    public h a() {
        return this.b;
    }

    public void a(b bVar) {
        this.d = bVar;
    }

    public void a(h hVar) {
        this.b = hVar;
    }

    public void a(i iVar) {
        this.c = iVar;
    }

    public void a(List<a> list) {
        if (list == null) {
            this.e = null;
        } else {
            this.e = (a[]) list.toArray(new a[list.size()]);
        }
    }

    public a[] b() {
        Object obj = this.e;
        if (obj == null) {
            return new a[0];
        }
        Object obj2 = new a[obj.length];
        System.arraycopy(obj, 0, obj2, 0, obj.length);
        return obj2;
    }

    public JSONObject c() {
        if (this.e == null) {
            return null;
        }
        JSONObject jSONObject = new JSONObject();
        try {
            JSONObject jSONObject2 = new JSONObject();
            if (this.b != null) {
                jSONObject.put("header", this.b.b());
            }
            if (!(this.c == null || this.d == null)) {
                JSONObject a = this.d.a();
                a.put("properties", this.c.a());
                Object f = c.f(this.f, this.g);
                if (TextUtils.isEmpty(f)) {
                    a.put("events_global_properties", "");
                } else {
                    a.put("events_global_properties", new JSONObject(f));
                }
                jSONObject2.put("events_common", a);
            }
            JSONArray jSONArray = new JSONArray();
            for (a b : this.e) {
                JSONObject b2 = b.b();
                if (b2 != null) {
                    jSONArray.put(b2);
                }
            }
            jSONObject2.put("events", jSONArray);
            byte[] a2 = i.a(jSONObject2.toString().getBytes("UTF-8"));
            byte[] a3 = d.a();
            jSONObject.put("event", d.a(a3, d.a(this.a, a3, a2)));
            return jSONObject;
        } catch (JSONException e) {
            b.c("UploadData", "event upload data - toJsonObj(): JSON Exception has happen");
            return jSONObject;
        } catch (UnsupportedEncodingException e2) {
            b.c("UploadData", "getBitZip(): Unsupported coding : utf-8");
            return jSONObject;
        }
    }
}
